#include "tools.h"

using namespace std;

unsigned int bt::nextPowerOf2(unsigned int num)
{
	int powerOf2 = 1;
	
	while(powerOf2 < num)
	{
		powerOf2 *= 2;
	}
	
	return powerOf2;
}

// String helper functions
void bt::trimString(string& str)
{
	string whiteSpace = " \t\r\n";
	
	string::size_type endSpace;
	
	endSpace = str.find_last_not_of(whiteSpace);
	
	if(endSpace != str.npos)
	{	/* erase whiteSpace at the end if there is any */
		str.erase(endSpace + 1);
		
		string::size_type startSpace = str.find_first_not_of(whiteSpace);
		
		if(startSpace != str.npos)
			str.erase(0, startSpace);
	}
	else
		str.erase(str.begin(), str.end());
}

string bt::cutFirstToken(string& str, const char* delim)
{
	string subStr;
	
	string::size_type pos = str.find(delim);
	
	if(pos != str.npos)
	{
		/* Get substring up to delimeter */
		subStr = str.substr(0, pos);

		/* Erase all up to the delimeter or end of string */
		str.erase(0, pos+1);
	}
	else
	{
		subStr = str;
		str.erase(str.begin(), str.end());
	}
		
	return subStr;
}
